package com.gezhi.stuserver.clsmag.controller;

import com.gezhi.stuserver.ClsFeignFallbackFactory;
import com.gezhi.stuserver.feign.ClsFeign;
import com.gezhi.stuserver.dto.ResultData;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import feign.hystrix.FallbackFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RequestMapping(value = "/stu/cls")
@RestController
public class ClassController {

	@Resource
	private ClsFeign classFeign;

	/**
	 * 查询所有的班级
	 * @return 所有的班级组成的List集合
	 */
	@HystrixCommand(commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
	}, fallbackMethod = "getInfoFallback")
	@GetMapping("/list")
	public ResultData findAllClass() {
		// 模拟超时
		//		try {
		//			Thread.sleep(10000);
		//		} catch (Exception e) {
		//			e.printStackTrace();
		//		}
		return classFeign.findAllClass();
	}

	/**
	 * 组成返回错误信息
	 */
	public ResultData getInfoFallback() {
		ResultData resultData = new ResultData();
		resultData.setCode("10000");
		resultData.setMsg("[服务降级]：班级系统业务繁忙(超时)，请稍后重试！");
		return resultData;
	}

}
